<?php

class searchActions extends sfActions {
    
    public function executeIndex(sfWebRequest $request){
        $category_url = $request->getParameter("category_url");
        $products = array();
        $products_only = array();
        $c = new Criteria();
        
        $c->clear();
        $c->addSelectColumn(ProductPeer::ID);//0
        $c->addSelectColumn(ProductPeer::NAME);
        $c->addSelectColumn(ProductPeer::PRICE);
        $c->addSelectColumn(ProductAttributePeer::ID);
        $c->addSelectColumn(ProductAttributePeer::PRODUCT_ID);
        $c->addSelectColumn(ProductAttributePeer::CUSTOM_ATTRIBUTE_ID);//5
        $c->addSelectColumn(ProductAttributePeer::CUSTOM_ATTRIBUTE_OPTION_ID);
        $c->addSelectColumn(ProductAttributePeer::TEXT_VALUE);
        $c->addSelectColumn(CustomAttributePeer::NAME);
        $c->addSelectColumn(CustomAttributeOptionPeer::VALUE);
        $c->addSelectColumn(TypePeer::GROUP);//10
        $c->addSelectColumn(TypePeer::VALUE);
        $c->addSelectColumn(TypePeer::NAME);
        $c->addJoin(ProductAttributePeer::PRODUCT_ID, ProductPeer::ID);
        $c->addJoin(ProductAttributePeer::CUSTOM_ATTRIBUTE_ID, CustomAttributePeer::ID);
        $c->addJoin(ProductAttributePeer::CUSTOM_ATTRIBUTE_OPTION_ID, CustomAttributeOptionPeer::ID, Criteria::LEFT_JOIN);
        $c->addJoin(ProductPeer::CATEGORY_ID, CategoryPeer::ID);
        $c->addJoin(CustomAttributePeer::FIELD_TYPE_ID, TypePeer::ID);
        
        //Condicionales
        if($category_url!=null){
            $c->add(CategoryPeer::URL, $category_url);
        }
        
        $productsStmt = ProductPeer::doSelectStmt($c);
        
        foreach($productsStmt as $productStmt){
            $products[] = $productStmt;
        }
        
        $this->products = $products;
        
        foreach($products as $row){
            $products_only_row = null;
            $products_only_row["id"] = $row[0];
            $products_only_row["name"] = $row[1];
            $products_only_row["price"] = $row[2];
            if(!in_array($products_only_row, $products_only)){
                $products_only[] = $products_only_row;
            }
        }
        $this->products_only = $products_only;
    }
    
}